/// <reference types="vitest" />
import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import postCssPxToRem from 'postcss-pxtorem'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
import legacy from '@vitejs/plugin-legacy'

// https://vitejs.dev/config/
export default ({ mode }: any) => {
  process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
  return defineConfig({
    base: process.env.VITE_APP_BASE_KEY,
    plugins: [
      vue(),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false
          })
        ]
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/svgs')],
        symbolId: 'icon-[dir]-[name]'
      }),
      legacy({
        // 浏览器兼容 添加后打包回很慢
        // targets: ['defaults', 'ie >= 11', 'chrome 53']
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    test: {
      environment: 'jsdom'
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          modifyVars: {
            hack: `true; @import (reference) "${fileURLToPath(
              new URL('./src/style/main.less', import.meta.url)
            )}";`
          }
        }
      },
      postcss: {
        plugins: [
          require('tailwindcss'),
          require('autoprefixer'),
          postCssPxToRem({
            rootValue: 16, // 1rem的大小（控制1rem的大小  点位：px）
            propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
            unitPrecision: 5
          })
        ]
      }
    },
    server: {
      proxy: {
        '/api': {
          target: 'http://10.0.101.75:18080/api', // 测试环境
          // target: 'http://10.0.102.75:18080/api', // 开发环境
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  })
}
